<?php
global $_GPC, $_W;
$GLOBALS['frames'] = $this->getMainMenu();
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$type=empty($_GPC['type']) ? 'all' :$_GPC['type'];
$state=$_GPC['state'];
$pageindex = max(1, intval($_GPC['page']));
$pagesize=10;
$where=' WHERE a.uniacid=:uniacid';
$data[':uniacid']=$_W['uniacid'];

if(isset($_GPC['keywords'])){
    $where.="  and (a.students_name LIKE  concat('%', :name,'%') or a.parents_name LIKE  concat('%', :name,'%'))";
    $data[':name']=$_GPC['keywords']; 
    $type='all';  
}

 if($type!='all'){
    $where.=" and a.state=".$state;
}

	$pagesize=10;
  $sql="SELECT a.*,b.class_name,d.course_name,e.cate_name FROM ".tablename('snx_users'). " a"  . " left join " . tablename("snx_class") . " b on a.class_id=b.id " . " left join " . tablename("snx_course") . " c on b.cou_id=c.id " . " left join " . tablename("snx_coursecate") . " d on c.c_id=d.id " . " left join " . tablename("snx_school") . " e on d.school_id=e.id ".$where." ORDER BY a.create_time desc";
	// $sql="select *  from " . tablename("snx_users") .$where;
	$select_sql =$sql." LIMIT " .($pageindex-1) * $pagesize.",".$pagesize;
	$list = pdo_fetchall($select_sql,$data);
  // echo json_encode($list,320);die;
	$total=pdo_fetchcolumn("SELECT count(*) FROM ".tablename('snx_users'). " a"  . " left join " . tablename("snx_class") . " b on a.class_id=b.id " . " left join " . tablename("snx_course") . " c on b.cou_id=c.id " . " left join " . tablename("snx_coursecate") . " d on c.c_id=b.id " . " left join " . tablename("snx_school") . " e on d.school_id=e.id ".$where." ORDER BY a.create_time desc",$data);
	$pager = pagination($total, $pageindex, $pagesize);

if($_GPC['op']=='delete'){
    $res=pdo_delete('snx_users',array('id'=>$_GPC['id']));
    if($res){
        message('删除成功！', $this->createWebUrl('user'), 'success');
    }else{
        message('删除失败！','','error');
    }
}
if($_GPC['op']=='tg'){
        function getaccess_token($_W){
          $res=pdo_get('snx_system',array('uniacid'=>$_W['uniacid']));
          $appid=$res['appid'];
          $secret=$res['appsecret'];
          $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL,$url);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
          $data = curl_exec($ch);
          curl_close($ch);
          $data = json_decode($data,true);
          return $data['access_token'];
        }
        function set_msgs($_W,$res_id){
            $access_token = getaccess_token($_W);
            $ress=pdo_get('snx_system',array('uniacid'=>$_W['uniacid']));
            $openid=pdo_get('snx_users',array('id'=>$res_id));
            $formwork ='{
                "touser": "'.$openid["openid"].'",
                "template_id": "'.$ress["templeidreg"].'",
                "form_id":"'.$openid['rformid'].'",
                "data": {
                    "keyword1": {
                        "value": "'.$openid['phone_number'].'",
                        "color": "#173177"
                    },
                    "keyword2": {
                        "value":"'.$openid['students_name'].'",
                        "color": "#173177"
                    },
                    "keyword3": {
                        "value":"注册成功，欢迎加入SUNNY英语大家庭!",
                        "color": "#173177"
                    }
                }  
            }';
            $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$access_token."";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,$url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
            curl_setopt($ch, CURLOPT_POST,1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$formwork);
            $data = curl_exec($ch);
            curl_close($ch);
           return $data;
        }
        set_msgs($_W,$_GPC['id']);
	$res=pdo_update('snx_users',array('state'=>2),array('id'=>$_GPC['id']));
	if($res){
     message('通过成功！', $this->createWebUrl('user'), 'success');
    }else{
          message('通过失败！','','error');
    }
}
if($_GPC['op']=='jj'){
	$res=pdo_update('snx_users',array('state'=>3),array('id'=>$_GPC['id']));
	if($res){
     message('拒绝成功！', $this->createWebUrl('user'), 'success');
    }else{
          message('拒绝失败！','','error');
    }
}
if($_GPC['op']=='change'){
	    $res = pdo_update('snx_users',array('state'=>$_GPC['state']),array('id'=>$_GPC['id']));
		if($res){
		 message('编辑成功！', $this->createWebUrl('user'), 'success');
		}else{
			  message('编辑失败！','','error');
		}
	}
include $this->template('web/user');